// pages/Artists/Artists.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        artistlist:[],
        arealist:[
            {
                name:'全部',
                area:-1
            },
            {
                name:'华语',
                area:7
            },
            {
                name:'欧美',
                area:96
            },
            {
                name:'日本',
                area:8
            },
            {
                name:'韩国',
                area:16
            },
            {
                name:'其他',
                area:0
            },
        ],
        namelist:[],
        typelist:[
            {
                type:-1,
                val:'全部'
            },
            {
                type:1,
                val:'男歌手'
            },
            {
                type:2,
                val:'女歌手'
            },
            {
                type:3,
                val:'乐队'
            }
        ],
        area:-1,
        initial:-1,
        type:-1,
        areaNum:0,
        initialNum:0,
        typeNum:0,
        limit:15,
        offset:0,
        isLoading:false
    },
    // 初始化字母
    initName(){
        let x = ['热门']
        for(let i = 0;i < 26;i++){
            x.push(String.fromCharCode(65+i))
        }
        this.setData({
            namelist:x
        })
    },
    // 改变语种选择
    changeArea(e){
        if(this.data.areaNum == e.currentTarget.dataset.index) return
        this.setData({
            areaNum:e.currentTarget.dataset.index,
            area:this.data.arealist[e.currentTarget.dataset.index].area
        }) 
        wx.showLoading({
          title: 'loading...',
        })
        this.setData({
            offset:0,
            artistlist:[]
        })
        this.getArtist()
    },
    // 改变字母选择
    changeInitial(e){
        if(this.data.initialNum == e.currentTarget.dataset.index) return
        this.setData({
            initialNum:e.currentTarget.dataset.index,
            initial:this.data.namelist[e.currentTarget.dataset.index]
        }) 
        if(this.data.initial === '热门') 
            this.setData({
                initial:-1
            })
            wx.showLoading({
                title: 'loading...',
              })
              this.setData({
                  offset:0,
                  artistlist:[]
              })
              this.getArtist()
    },
    // 改变种类选择
    changeType(e){
        if(this.data.typeNum == e.currentTarget.dataset.index) return
        this.setData({
            typeNum:e.currentTarget.dataset.index,
            type:this.data.typelist[e.currentTarget.dataset.index].type
        }) 
        wx.showLoading({
            title: 'loading...',
          })
          this.setData({
              offset:0,
              artistlist:[]
          })
          this.getArtist()
    },
    // 获取歌手信息
    async getArtist(){
        this.setData({
            isLoading:true
        })
        const res = await wx.p.request({
            url:`${wx.root}/artist/list`,
            data:{
                type:this.data.type,
                area:this.data.area,
                initial:this.data.initial,
                limit:this.data.limit,
                offset:this.data.offset*this.data.limit
            }
        })
        this.setData({
            artistlist:[...this.data.artistlist,...res.data.artists],
            offset:this.data.offset+1,
            isLoading:false,
            limit:24
        })
        wx.hideLoading({
          success: (res) => {},
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initName()
        this.getArtist()
    },
    onReachBottom: function () {
        if(this.data.isLoading == false)
            this.getArtist()
    },
})